﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Repository;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Dto.YS;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Model.Wcs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace DFGK_WMS.WebAPI.Controllers.PC.设备管理
{
    /// <summary>
    /// 设备管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class WCSController : BaseController
    {
        private readonly IShippingReplenishmentService shippingReplenishmentService;
        private readonly IReceivingOrderDetailService receivingOrderDetailService;
        private readonly IShippingOrderDetailService shippingOrderDetailService;
        private readonly ITransferBinOrderService transferBinOrderService;
        private readonly IStocktakingOrderService stocktakingOrderService;
        private readonly IPutawayOrderRepository putawayOrderRepository;
        private readonly IShippingOrderDetailRepository _shippingOrderDetailRepository;
        private readonly IShippingLockCellRepository _shippingLockCellRepository;
        
        private readonly WcsHttpClient _wcsHttpClient;
        private readonly IReceivingBillRepository _receivingBillRepository;
        private readonly IReceivingBillDetailRepository _receivingBillDetailRepository;
        private readonly IInventoryRepository _inventoryRepository;
        private readonly IWarehouseRepository _warehouseRepository;
        private readonly IPickingOrderRepository pickingOrderRepository;
        private readonly IInterfaceLogService interfaceLogService;
        private readonly IBarCodeRuleService barCodeRuleService;
        private readonly IReceivingOrderDetailRepository _receivingOrderDetailRepository;
        private readonly ICellService cellService;
        private readonly IUnitOfWork unitOfWork;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="receivingOrderDetailService"></param>
        /// <param name="shippingOrderDetailService"></param>
        /// <param name="transferBinOrderService"></param>
        /// <param name="shippingReplenishmentService"></param>
        /// <param name="putawayOrderRepository"></param>
        /// <param name="pickingOrderRepository"></param>
        /// <param name="cellService"></param>
        /// <param name="interfaceLogService"></param>
        /// <param name="stocktakingOrderService"></param>
        /// <param name="unitOfWork"></param>
        /// <param name="barCodeRuleService"></param>
        public WCSController(IWebHostEnvironment environment, IReceivingOrderDetailService receivingOrderDetailService, IShippingOrderDetailService shippingOrderDetailService, ITransferBinOrderService transferBinOrderService, IShippingReplenishmentService shippingReplenishmentService, IPutawayOrderRepository putawayOrderRepository, IPickingOrderRepository pickingOrderRepository, ICellService cellService, IInterfaceLogService interfaceLogService, IStocktakingOrderService stocktakingOrderService, IUnitOfWork unitOfWork, IBarCodeRuleService barCodeRuleService, WcsHttpClient wcsHttpClient, IInventoryRepository inventoryRepository, IReceivingBillRepository receivingBillRepository, IReceivingBillDetailRepository receivingBillDetailRepository, IWarehouseRepository warehouseRepository, IReceivingOrderDetailRepository receivingOrderDetailRepository, IShippingOrderDetailRepository shippingOrderDetailRepository, IShippingLockCellRepository shippingLockCellRepository) : base(environment)
        {
            this.shippingReplenishmentService = shippingReplenishmentService;
            this.receivingOrderDetailService = receivingOrderDetailService;
            this.shippingOrderDetailService = shippingOrderDetailService;
            this.transferBinOrderService = transferBinOrderService;
            this.stocktakingOrderService = stocktakingOrderService;
            this.putawayOrderRepository = putawayOrderRepository;
            this.pickingOrderRepository = pickingOrderRepository;
            this.interfaceLogService = interfaceLogService;
            this.barCodeRuleService = barCodeRuleService;
            this.cellService = cellService;
            this.unitOfWork = unitOfWork;
            _wcsHttpClient = wcsHttpClient;
            _inventoryRepository = inventoryRepository;
            _receivingBillRepository = receivingBillRepository;
            _receivingBillDetailRepository = receivingBillDetailRepository;
            _warehouseRepository = warehouseRepository;
            _receivingOrderDetailRepository = receivingOrderDetailRepository;
            _shippingOrderDetailRepository = shippingOrderDetailRepository;
            _shippingLockCellRepository = shippingLockCellRepository;
        }

        /// <summary>
        /// 任务上报
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult TasklikuAppear(TaskDto m)
        {
            if (string.IsNullOrEmpty(m.TaskCode))
            {
                return Ok(BaseResult.Result("400", "参数有误！"));
            }
            string message = "";
            //if (m.Method != "End")
            //{
            //    try
            //    {
            //        InterfaceLog interfaceLog = new InterfaceLog();
            //        interfaceLog.Name = "WCS";
            //        interfaceLog.Url = "WCS/TasklikuAppear";
            //        interfaceLog.CallWay = 2;
            //        interfaceLog.CallWayStr = "POST";
            //        interfaceLog.InParam = JsonConvert.SerializeObject(m);
            //        interfaceLog.OutParam = m.Method;
            //        interfaceLog.ExecDate = DateTime.Now;
            //        interfaceLog.FinishedDate = DateTime.Now;
            //        interfaceLog.Status = 3;
            //        interfaceLogService.AddInterfaceLog(interfaceLog, out message);
            //        unitOfWork.SaveChangesEntity();
            //    }
            //    catch (Exception)
            //    {
            //        return Ok(BaseResult.Result(false, "操作失败！" + message, "500"));
            //    }
            //    return Ok(BaseResult.Result(false, "操作成功！" + message, "200"));
            //}
            bool result = false;
            string end = m.TaskCode.Substring(0, 3);
            if (end == "PUT")
                 result =  receivingOrderDetailService.FinishReceivingOrderDetail(m.TaskCode, out message);
            else if (end == "OUT")
                 result =  shippingOrderDetailService.FinishShippingOrderDetail(m.TaskCode, out message);
            else if (end == "ORT")
                result = shippingOrderDetailService.FinishShippingOutZeroQty(m.TaskCode, out message);
            else if (end == "SRT")
                result = shippingOrderDetailService.FinishPickingShippingOrderDetail(m.TaskCode, out message);
            else if (end == "TRA")
                result = transferBinOrderService.FinishTransferBinOrder(m.TaskCode, out message);
            else if (end == "REP")
                result = shippingReplenishmentService.FinishReplenishment(m.TaskCode, out message);
            else if (end == "KTP")
                result = shippingOrderDetailService.FinishEmptyPalletBarcode(m.TaskCode, out message);
            else if (end == "STO")
                result = stocktakingOrderService.FinishStocktakingOrder(m.TaskCode, out message);
            else if (end == "STP")
                result = stocktakingOrderService.FinishStocktakingOrders(m.TaskCode, out message);
            if (result)
            {
               
                return Ok(BaseResult.Result(result, "操作成功！" + message, "200"));
            }
            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }

        /// <summary>
        /// 强制完成
        /// </summary>
        /// <param name="palletcode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ForceCompletion(string palletcode)
        {
            if (string.IsNullOrEmpty(palletcode))
            {
                return Ok(BaseResult.Result("400", "参数有误！"));
            }
            string code = string.Empty;
            var putobj = putawayOrderRepository.QueryEntityWhere(x => x.PalletBarcode == palletcode && x.Status != 5)?.Code;
            if (putobj != null)
            {
                code = putobj;
            }
            else
            {
                var pickobj = pickingOrderRepository.QueryEntityWhere(x => x.PalletBarcode == palletcode && x.Status != 5)?.Code;
                if (pickobj != null)
                    code = pickobj;
                else
                    return Ok(BaseResult.Result(false, "操作失败！找不到任务！", "500"));
            }
            bool result = false;
            string message = string.Empty;
            string end = code.Substring(0, 3);
            
            if (end == "PUT")
                 result =  receivingOrderDetailService.FinishReceivingOrderDetail(code, out message);
            else if (end == "OUT")
                 result =  shippingOrderDetailService.FinishShippingOrderDetail(code,out message);
            else if (end == "ORT")
                result = shippingOrderDetailService.FinishShippingOutZeroQty(code, out message);
            else if (end == "SRT")
                result = shippingOrderDetailService.FinishPickingShippingOrderDetail(code, out message);
            else if (end == "TRA")
                result = transferBinOrderService.FinishTransferBinOrder(code, out message);
            else if (end == "REP")
                result = shippingReplenishmentService.FinishReplenishment(code, out message);
            else if (end == "KTP")
                result = shippingOrderDetailService.FinishEmptyPalletBarcode(code, out message);
            else if (end == "STO")
                result = stocktakingOrderService.FinishStocktakingOrder(code, out message);
            else if (end == "STP")
                result = stocktakingOrderService.FinishStocktakingOrders(code, out message);
            if (result)
            {
                if (!Config.IsDev)
                {
                    ChangeTaskDto changetask = new ChangeTaskDto
                    {
                        TaskCode = code,
                        Val = 4
                    };
                    WcsTaskResultDto? wcsresult2 = _wcsHttpClient.ChangeTask(changetask);

                }
                return Ok(BaseResult.Result(result, "操作成功！" + message, "200"));
            }
            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }

        /// <summary>
        /// 其他任务上报
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StationNotice(StationOtherDto m)
        {
            string message = "";
            if (string.IsNullOrEmpty(m.StationNoticeType))
            {
                return Ok(BaseResult.Result("400", "类型不能为空！"));
            }
            bool result = false;
            WcsTaskResultDtos wcsTaskResultDtos = new WcsTaskResultDtos();
            //整托入库
            if (m.StationNoticeType == "S101")
            {
                var pickobj = pickingOrderRepository.QueryEntityWhere(x => x.Status != 5);
                if (pickobj != null)
                    return Ok(BaseResult.Result(null, "保存失败！有出库任务正在执行！", "500"));
                var putobj = putawayOrderRepository.QueryEntityWhere(x => x.Status != 5 && x.PalletBarcode == m.PalletCode);
                if (putobj == null)
                {
                    return Ok(BaseResult.Result(wcsTaskResultDtos, "操作失败！找不到上架单！", "500"));
                }
                wcsTaskResultDtos.TaskCode = putobj.Code;
                // 假设 putobj.LocationCode 已经是 "Cell:3_4_13_5_2"
                // 从索引5开始截取字符串，并分割
                string[] parts = putobj.LocationCode.Substring(5).Split(separator: '_');
                wcsTaskResultDtos.Tunnel = Convert.ToInt32(parts[0]); // 巷道是第一个数字
                wcsTaskResultDtos.Row = Convert.ToInt32(parts[1]);    // 排是第二个数字
                wcsTaskResultDtos.Bay = Convert.ToInt32(parts[2]);    // 列是第三个数字
                wcsTaskResultDtos.Level = Convert.ToInt32(parts[3]);  // 层是第四个数字

                if (wcsTaskResultDtos.Tunnel == 3)
                    wcsTaskResultDtos.Station = EmStation.S107;
                else
                    wcsTaskResultDtos.Station = EmStation.S106;
                wcsTaskResultDtos.Count = putobj.Qty;
                return Ok(BaseResult.Result(wcsTaskResultDtos, "操作成功！" + message, "200"));
            }
            //申请出空托盘到暂存区
            else if (m.StationNoticeType == "S104")
            {
                var putobj = putawayOrderRepository.QueryEntityWhere(x => x.Status != 5);
                if (putobj != null)
                    return Ok(BaseResult.Result(null, "保存失败！有入库任务正在执行！", "500"));
                result = shippingOrderDetailService.GetEmptyPalletBarcode(out message);
                if (result)
                {
                    var pickobj = pickingOrderRepository.QueryEntityWhere(x => x.Status != 5 && x.PalletBarcode == message);
                    if (pickobj == null)
                    {
                        return Ok(BaseResult.Result(wcsTaskResultDtos, "操作失败！" + message, "500"));
                    }
                    string[] parts = pickobj.LocationCode.Substring(5).Split(separator: '_');
                    wcsTaskResultDtos.TaskCode = pickobj.Code;
                    wcsTaskResultDtos.Tunnel = Convert.ToInt32(parts[0]); // 巷道是第一个数字
                    wcsTaskResultDtos.Row = Convert.ToInt32(parts[1]);    // 排是第二个数字
                    wcsTaskResultDtos.Bay = Convert.ToInt32(parts[2]);    // 列是第三个数字
                    wcsTaskResultDtos.Level = Convert.ToInt32(parts[3]);  // 层是第四个数字
                    if (wcsTaskResultDtos.Tunnel == 3)
                        wcsTaskResultDtos.Station = EmStation.S107;
                    else
                        wcsTaskResultDtos.Station = EmStation.S106;
                    wcsTaskResultDtos.Count = pickobj.Qty;
                    return Ok(BaseResult.Result(wcsTaskResultDtos, "操作成功！" + message, "200"));
                }
                else
                {
                    return Ok(BaseResult.Result(wcsTaskResultDtos, message, "500"));
                }
            }
            //申请回库
            else if (m.StationNoticeType == "S205")
            {
                var pickobj = pickingOrderRepository.QueryEntityWhere(x => x.Status != 5);
                if (pickobj != null)
                    return Ok(BaseResult.Result(null, "保存失败！有出库任务正在执行！", "500"));
                var putobj = putawayOrderRepository.QueryEntityWhere(x => x.Status != 5 && x.PalletBarcode == m.PalletCode);
                if (putobj == null)
                {
                    return Ok(BaseResult.Result(wcsTaskResultDtos, "操作失败！" + message, "500"));
                }
                string[] parts = putobj.LocationCode.Substring(5).Split(separator: '_');
                wcsTaskResultDtos.TaskCode = putobj.Code;
                wcsTaskResultDtos.Tunnel = Convert.ToInt32(parts[0]); // 巷道是第一个数字
                wcsTaskResultDtos.Row = Convert.ToInt32(parts[1]);    // 排是第二个数字
                wcsTaskResultDtos.Bay = Convert.ToInt32(parts[2]);    // 列是第三个数字
                wcsTaskResultDtos.Level = Convert.ToInt32(parts[3]);  // 层是第四个数字
                if (wcsTaskResultDtos.Tunnel == 3)
                    wcsTaskResultDtos.Station = EmStation.S107;
                else
                    wcsTaskResultDtos.Station = EmStation.S106;
                wcsTaskResultDtos.Count = putobj.Qty;
                return Ok(BaseResult.Result(wcsTaskResultDtos, "操作成功！" + message, "200"));
            }
            //空托盘租申请入库
            else if (m.StationNoticeType == "S207")
            {
                var pickobj = pickingOrderRepository.QueryEntityWhere(x => x.Status != 5);
                if (pickobj != null)
                    return Ok(BaseResult.Result(null, "保存失败！有出库任务正在执行！", "500"));
                result = receivingOrderDetailService.GetEmptyPalletBarcode(m.PalletCode, m.Count, out message);
                if (result)
                {
                    var putobj = putawayOrderRepository.QueryEntityWhere(x => x.Status != 5 && x.PalletBarcode == message);
                    if (putobj == null)
                    {
                        return Ok(BaseResult.Result(wcsTaskResultDtos, "操作失败！" + message, "500"));
                    }
                    string[] parts = putobj.LocationCode.Substring(5).Split(separator: '_');
                    wcsTaskResultDtos.TaskCode = putobj.Code;
                    wcsTaskResultDtos.Tunnel = Convert.ToInt32(parts[0]); // 巷道是第一个数字
                    wcsTaskResultDtos.Row = Convert.ToInt32(parts[1]);    // 排是第二个数字
                    wcsTaskResultDtos.Bay = Convert.ToInt32(parts[2]);    // 列是第三个数字
                    wcsTaskResultDtos.Level = Convert.ToInt32(parts[3]);  // 层是第四个数字
                    if (wcsTaskResultDtos.Tunnel == 3)
                        wcsTaskResultDtos.Station = EmStation.S107;
                    else
                        wcsTaskResultDtos.Station = EmStation.S106;
                    wcsTaskResultDtos.Count = putobj.Qty;
                    return Ok(BaseResult.Result(wcsTaskResultDtos, "操作成功！" + message, "200"));
                }
                else
                {
                    return Ok(BaseResult.Result(wcsTaskResultDtos, message, "500"));
                }
            }
            else
            {
                return Ok(BaseResult.Result(wcsTaskResultDtos, "无对应StationNoticeType！" + message, "500"));
            }
        }

        /// <summary>
        /// 入库区 条码上报, WMS收到条码后和指令绑定
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BindFaterPallet(WcsBindPalletDto m)
        {
            string message = string.Empty;
            var result = false;
            if (!string.IsNullOrEmpty(m.PalletCode))
            {
                var putawayOrder = putawayOrderRepository.QueryEntityWhere(d => d.Code == m.TaskCode && d.Status == 4);
                if (putawayOrder != null)
                {
             
                    var inventory = _inventoryRepository.QueryEntityWhere(d => d.RelatedOrderDetailId == putawayOrder.RelatedOrderDetailId);
                    if (inventory == null)
                    {
                        message = "上架单关联库存不存在！";
                        result = false;
                    }
                    inventory.ParentPalletBarcode = m.PalletCode;

                    _inventoryRepository.UpdEntity(inventory);
                    //var receOrderDetail = _receivingOrderDetailRepository.QueryEntityWhere(d => d.Id == inventory.RelatedOrderDetailId);
                    //if (receOrderDetail == null)
                    //{
                    //    message = "库存关联入库单不存在！";
                    //    result = false;
                    //}

                    //receOrderDetail.PalletBarcode = m.PalletCode;
                    result = unitOfWork.SaveChangesEntity();

                    return Ok(BaseResult.Result(new { result, message }, "操作成功！" + message, "200"));
                }
                message = "上架单不存在！";
                result = false;
                return Ok(BaseResult.Result(new { result, message }, "操作失败！" + message, "500"));
            }
            message = "未传入父托盘";
            return Ok(BaseResult.Result(new { result,message}, "操作失败！" + message, "500"));
        }
        /// <summary>
        /// 叠盘机入库申请/拆盘机出空托盘申请 , WMS需要生成一条指令表记录并把必要信息返给WCS
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        
        [HttpPost]
        public IActionResult DiscStackingMachine(TaskDto mm)
        {
            var message = string.Empty;
            var result = false;
            var result1 = false;
            //pallerCode值为 - 1时为 拆盘机出库申请，反之则是叠盘机入库申请
            if (!string.IsNullOrEmpty(mm.PalletCode))
            {
                if (mm.PalletCode == "-1")
                {
                    ErpShippingOrderDTO shiporder = new ErpShippingOrderDTO
                    {
                        BillNo = "CP" + DateTime.Now.ToString("yyyyMMddHHmmss"),
                        ProductName = "空托盘",
                        items = new List<ErpDocumentsDTOListDTO>()
                    };
                    ErpDocumentsDTOListDTO documentsdto = new ErpDocumentsDTOListDTO
                    {
                        MaterialCode= "Pallet",
                        MaterialName= "空托盘",
                        Qty=1,
                    };
                    shiporder.items.Add(documentsdto);
                    //调用分配库存
                   result= shippingOrderDetailService.ERPAddShippingOrderDetail(shiporder,out message);
                    if (result)
                    {
                        var shippingOrderDetail = _shippingOrderDetailRepository.QueryEntityWhere(d => d.OrderSn == shiporder.BillNo);
                        if (shippingOrderDetail == null)
                        {
                            result = false;
                            message = "空托盘出库找不到出库单子";
                            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
                        }
                        //查相关的出库单明细
                        var shippingLockCell = _shippingLockCellRepository.QueryEntityWhere(d => d.OrderDetailId == shippingOrderDetail.Id);
                        //查到相关的库存信息
                        var inventory = _inventoryRepository.QueryEntityWhere(d => d.Id == shippingLockCell.InventoryId);

                        List<SubmitOutListDTO> list = new List<SubmitOutListDTO>();
                        SubmitOutListDTO outListDTO = new SubmitOutListDTO
                        {
                            Qty = 1,
                            PalletBarcode = "0",
                            OrderDetId = shippingLockCell.Id
                        };
                        list.Add(outListDTO);
                        //调用开始出库
                        var putobj = putawayOrderRepository.QueryEntityWhere(x => x.Status != 5);
                        if (putobj != null)
                            return Ok(BaseResult.Result(false, "保存失败！有入库任务正在执行！", "500"));

                         var list1 = shippingOrderDetailService.GetCellDepth(list);
                        result1 = shippingOrderDetailService.StartShippingOrderDetail(list1, out message);
                        if (!result1)
                        {
                            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
                        }
                        var pickorder = pickingOrderRepository.QueryEntityWhere(d => d.RelatedOrderDetailId == shippingLockCell.Id);

                        return Ok(BaseResult.Result(new { TaskCode = pickorder.Code, PalletCode = inventory.PalletBarcode, CellCode = inventory.ErplocationCode }, "操作成功！仓位为" + message, "200"));
                    }
                    return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
                }
                else
                {

                  var warehouse=  _warehouseRepository.QueryEntityWhere(d => d.Id == 666038277042181);
                    ReceivingBill receivingBill = new ReceivingBill
                    {
                        BillNo = "DP" + DateTime.Now.ToString("yyyyMMddHHmmss"),
                        WarehouseId = 666038277042181,//成品库
                        WarehouseCode= warehouse.Code,
                        WarehouseName=warehouse.Name,
                        TotalQty=1,
                        BillDetailList = new List<ReceivingBillDetail>()
                    };
                    
                    unitOfWork.BeginTransaction();
                    _receivingBillRepository.AddEntity(receivingBill);
                    unitOfWork.SaveChangesEntity();
                    ReceivingBillDetail receivingBillDetail = new ReceivingBillDetail
                    {
                        MaterialId = 609393181765637,
                        MaterialName = "空托盘",
                        MaterialCode = "Pallet",
                        Qty = 1,
                        BatchNo = "",
                        BillId= receivingBill.Id,
                        BillNo= receivingBill.BillNo
                    };
                    receivingBill.BillDetailList.Add(receivingBillDetail);
                    _receivingBillDetailRepository.AddEntity(receivingBillDetail);

                    result = unitOfWork.SaveChangesEntity();
                    Console.WriteLine(receivingBill);
                    ReceivingBillOrderDto m = new ReceivingBillOrderDto();
                    m.Id = receivingBill.Id;
                    m.AId = 0;
                    m.PalletBarcode = mm.PalletCode;
                    m.ParentPalletBarcode = "0";
                    m.ReceivedQty = 1;
                    m.IsCreatedChecklist = false;

                    if (result)
                    {

                        unitOfWork.CommitChanges();
                        var result2 = receivingOrderDetailService.AddReceivingOrderDetail(m, out message);
                        var receivingOrderDetail=_receivingOrderDetailRepository.QueryEntityWhere(d => d.ReceivingBillDetailId == receivingBillDetail.Id);
                        var putaway = putawayOrderRepository.QueryEntityWhere(d => d.RelatedOrderDetailId == receivingOrderDetail.Id);
                        var inventory=_inventoryRepository.QueryEntityWhere(d => d.RelatedOrderDetailId == receivingOrderDetail.Id);
                        if (!result2)
                        {
                            return Ok(BaseResult.Result(result2, "操作失败！" + message, "500"));
                        }
                        return Ok(BaseResult.Result(new {TaskCode=putaway.Code,PalletCode= inventory.PalletBarcode, CellCode=inventory.ErplocationCode }, "操作成功！仓位为" + message, "200"));
                    }
                    else
                    {
                        unitOfWork.RollbackChanges(); // 添加回滚操作
                        return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
                    }


                }
            }

            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }

        /// <summary>
        /// 入库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BoxHasRead(TaskReceivingDto m)
        {
            string message = "";
            //不等于空分滑道/等于空是入库
            if (!string.IsNullOrEmpty(m.BoxData))
            {
                return Ok(receivingOrderDetailService.GetPalletMaterialCodeBySlide(m, out message));
            }
            else
            {
                return Ok(receivingOrderDetailService.GetLocationCodeByPalletCode(m.BoxNumber, out message));
            }
        }

        /// <summary>
        /// Wcs请求出库任务
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult WCSOutTask(OutTaskNoticeReqDto m)
        {
            try
            {
                //获取一个出库单号
                string? result = barCodeRuleService.GetBarCodeSample(2);
                if (string.IsNullOrEmpty(value: result))
                    return Ok(BaseResult.Result(false, "500", "获取出库单号失败！可能已被禁用"));
                m.OrderSn = result;
                var putobj = putawayOrderRepository.QueryEntityWhere(x => x.Status != 5);
                if (putobj != null)
                    return Ok(BaseResult.Result(false, "保存失败！有入库任务正在执行！", "500"));
                var list = shippingOrderDetailService.WCSOutTask(m, out string message);
                if(list == null)
                {
                    InterfaceLog interfaceLog = new InterfaceLog();
                    interfaceLog.Name = "WCS";
                    interfaceLog.Url = "WCS/WCSOutTask";
                    interfaceLog.CallWay = 2;
                    interfaceLog.CallWayStr = "POST";
                    interfaceLog.InParam = JsonConvert.SerializeObject(m);
                    interfaceLog.OutParam = message;
                    interfaceLog.ExecDate = DateTime.Now;
                    interfaceLog.FinishedDate = DateTime.Now;
                    interfaceLog.Status = 3;
                    interfaceLogService.AddInterfaceLog(interfaceLog, out message);
                    unitOfWork.SaveChangesEntity();
                    return Ok(BaseResult.Result(null, interfaceLog.OutParam, "500"));
                }           
                return Ok(BaseResult.Result(list, "操作成功！" + message, "200"));
            }
            catch (Exception e)
            {
                return Ok(BaseResult.Result(null, e.Message, "500"));
            }
        }
        [HttpPost]
        public IActionResult Test()
        {
            //return BaseResult.Fail();
            return BaseResult.Success();
        }
    }
}
